<template>
  <div class="search-suggestion">
    <van-cell icon="search" v-for="(value, index) in suggections" :key="index" @click="$emit('search', value)">
      <div slot="title" v-html="hightlight(value)"></div>
    </van-cell>
  </div>
</template>

<script>
import { debounce } from 'lodash'
import { getSearchSuggestions } from '@/api/search'
export default {
  name: 'SearchSuggestion',
  components: {},
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      suggections: []
    }
  },
  computed: {},
  watch: {
    searchText: {
      handler: debounce(async function () {
        const { data } = await getSearchSuggestions(this.searchText)
        this.suggections = data.data.options
        this.suggections = this.suggections.filter(item => {
          return item
        })
        console.log(this.suggections, 'cvwefvjg')
      }, 500),
      immediate: true
    }
  },
  created () { },
  mounted () { },
  methods: {
    hightlight (value) {
      console.log(value, '联想建议值')
      if (!value) return
      return value.replace(
        new RegExp(this.searchText, 'gi'),
        `<span style="color:red">${this.searchText}</span>`
      )
    }
  }
}
</script>

<style scoped lang="less"></style>
